package cn.t.listener;

import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.dto.order.ExcelBatchOnShelfDTO;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author l
 */
@Component
public class BatchOnShelfListener extends AnalysisEventListener<ExcelBatchOnShelfDTO> {

    //加载之前判断sku是否重复
    @Override
    public void invoke(ExcelBatchOnShelfDTO data, AnalysisContext context) {
        String err = data.checkParam();
        if (StringUtils.isNotBlank(err)) {
            int a=context.readRowHolder().getRowIndex()+1;
            String errStart="请检查第" + a + "行数据，";
            data.setErrLog(errStart.concat(err));
        }
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        // 如果异常是ExcelDataConvertException，则是格式错误
        if (exception instanceof ExcelDataConvertException) {
            throw new BizException(ResponseError.DATA_ERR ,"格式错误：请检查第" + context.readRowHolder().getRowIndex() + "行数据。" + "行数据的格式是否正确。");
        } else {
            // 其他类型的异常
            exception.printStackTrace();
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }
}